package com.ivt.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.ivt.core.ex.IvtException;
import com.ivt.dao.AttendanceDao;
import com.ivt.dao.SpecificDao;
import com.ivt.model.Attendance;
import com.ivt.model.Courses;
import com.ivt.model.Teacher;
import com.ivt.model.view.QueryCriteria;
import com.ivt.service.AttendanceService;
import com.ivt.utils.enums.AuditStatus;
import com.ivt.utils.enums.Sign;

@Service
public class AttendanceServiceImpl implements AttendanceService {

	@Resource
	private AttendanceDao attendancedao;

	@Resource
	private SpecificDao specificDao;

	@Override
	public Attendance create(Attendance attendance) throws IvtException {
		if (null == attendance) {
			throw new IvtException("出勤信息不能为空");
		}
		if (null == attendance.getStudent()) {
			throw new IvtException("出勤人ID不能为空");
		}
		if (StringUtils.isBlank(attendance.getAddress())) {
			throw new IvtException("出勤地址不能为空");
		}

		long c = attendancedao.existsByStudent(attendance.getCourses(),
				attendance.getStudent(), attendance.getSign());

		if (0 < c) {
			String msg = attendance.getSign().equals(Sign.IN) ? "您已经签过到了，不需要重复签到"
					: "您已经签退过了，不需要重复签退";
			throw new IvtException(msg);
		}

		attendance.setAttendanceId(null);
		// attendance.setStatus(AuditStatus.PASS);
		return attendancedao.saveAndFlush(attendance);
	}

	@Override
	public Attendance update(Attendance attendance) throws IvtException {
		if (null == attendance || null == attendance.getAttendanceId()
				|| 0 >= attendance.getAttendanceId()) {
			throw new IvtException("班级信息不能为空");
		}

		return attendancedao.saveAndFlush(attendance);

	}

	@Override
	public long queryCount(QueryCriteria queryCriteria) throws IvtException {
		queryCriteria = cleanQueryCriteria(queryCriteria);
		return specificDao.queryAttendanceCount(queryCriteria);
	}

	@Override
	public List<Attendance> queryClassesByCriteria(QueryCriteria queryCriteria)
			throws IvtException {
		queryCriteria = cleanQueryCriteria(queryCriteria);
		return specificDao.queryAttendance(queryCriteria);
	}

	@Override
	public Attendance delete(Long attendanceId) throws IvtException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deletes(Long[] attendanceIds) throws IvtException {
		if (ArrayUtils.isEmpty(attendanceIds)) {
			throw new IvtException("参数错误");
		}

		Set<Long> cleanIds = Sets.newHashSet(attendanceIds);
		Iterable<Attendance> attendance = null;
		attendance = attendancedao.findAll(cleanIds);
		int i = 0;
		if (null != attendance) {
			for (Attendance cz : attendance) {
				// cz.setStatus(AuditStatus.NOT);
				attendancedao.saveAndFlush(cz);
				i++;
			}
		}
		return i;
	}

	@Override
	public Attendance getAttendance(Long attendanceId) throws IvtException {
		if (null == attendanceId || 0 >= attendanceId) {
			throw new IvtException("参数错误");
		}
		return attendancedao.findOne(attendanceId);
	}

	@Override
	public List<Attendance> getAttendance(Set<Long> attendanceId)
			throws IvtException {
		if (CollectionUtils.isEmpty(attendanceId)) {
			throw new IvtException("参数错误");
		}

		Iterable<Attendance> coll = attendancedao.findAll(attendanceId);
		return (null == coll) ? null : Lists.newArrayList(coll);
	}

	protected QueryCriteria cleanQueryCriteria(QueryCriteria queryCriteria) {

		if (null == queryCriteria) {
			return null;
		}
		Map<String, Object> criteria = queryCriteria.splitCriteria();

		if (MapUtils.isNotEmpty(criteria)) {
			if (criteria.containsKey("teacher")
					&& StringUtils.isNumeric(String.valueOf(criteria
							.get("teacher")))) {
				Teacher teacher = new Teacher();
				teacher.setId(Long.valueOf(String.valueOf(criteria
						.get("teacher"))));
				criteria.put("teacher", teacher);
			}
			if (criteria.containsKey("courses")
					&& StringUtils.isNumeric(String.valueOf(criteria
							.get("courses")))) {
				Courses courses = new Courses();
				courses.setId(Long.valueOf(String.valueOf(criteria
						.get("courses"))));
				criteria.put("courses", courses);
			}
			if (criteria.containsKey("sign")
					&& StringUtils.isNumeric(String.valueOf(criteria
							.get("sign")))) {
				Sign sign = Sign.findByValue(Integer.valueOf(String
						.valueOf(criteria.get("sign"))));
				criteria.put("sign", sign);
			}
			if (criteria.containsKey("status")
					&& StringUtils.isNumeric(String.valueOf(criteria
							.get("status")))) {
				AuditStatus status = AuditStatus.findByValue(Integer
						.valueOf(String.valueOf(criteria.get("status"))));
				criteria.put("status", status);
			}
		}

		return queryCriteria;
	}
}
